package mess;

import java.util.*;

public class LeeCode_31 {
    public static void main(String[] args) {
        int[] nums = new int[]{1,3,2};
        nextPermutation(nums);
        System.out.println(Arrays.toString(nums));
    }

    private static void nextPermutation(int[] nums) {
        int len = nums.length;
        for(int i = len - 1;i > 0;i--){
            if(nums[i] > nums[i - 1]){
                int index = -1;
                for(int j = len - 1;j >= i;j--){
                    if (nums[j] > nums[i - 1]){
                        index = j;
                        break;
                    }
                }
                int tmp = nums[i - 1];
                nums[i - 1] = nums[index];
                nums[index] = tmp;
                Arrays.sort(nums, i, len);
                break;
            }else if(i == 1){
                Arrays.sort(nums);
                break;
            }
        }
    }
}
